require(["config"],function(){

    require(["jquery","template","header", "carousel"],function($,template){
        function Index(){
            this.init();
        }

        $.extend(Index.prototype,{
            constructor:Index,

            init: function(){
                this.renderList();
                this.loadCarousel();
                this.buysLoad();
                this.timer();
                this.addListener();
            },

            renderList: function(){
                $.ajax("http://rap2api.taobao.org/app/mock/86521/test")
                    .done(function(data){
                        var data = {list : data.res_body.list};

                        var html = template("list_template",data);
                        $(".prod_list").html(html);
                    });
            },

            addListener: function(){
                $(window).scroll(this.floornav);
                $(".one").on("click",function(){
                    $("html,body").animate({
                        scrollTop:$(".buy60s").offset().top + "px"
                    });
                });
                $(".two").on("click",function(){
                    $("html,body").animate({
                        scrollTop:$(".hot-activity").offset().top + "px"
                    });
                });
                $(".three").on("click",function(){
                    // console.log($(".footer-search").offset())
                    $("html,body").animate({
                        scrollTop:$(".recommend").offset().top + "px"
                    });
                });
            },
            floornav: function(){
                // console.log($("body,html").scrollTop())
                // console.log($(".buy60s").offset());
                var bodyTop = $("body,html").scrollTop(),
                    _floor = $(".floor"),
                    buystop = $(".buy60s").offset().top,
                    hot_activity = $(".hot-activity").offset().top,
                    recommend = $(".recommend").offset().top,
                    footer_search = $(".footer-search").offset().top;
                if(bodyTop >= buystop && bodyTop < hot_activity){
                    _floor.fadeIn(200);
                    $(".floor div").css({
                        "background":"",
                        "color":"#cccccc"
                    });
                    $(".one").css({
                        "background":"#ff666b",
                        "color":"white"
                    });
                }else if(bodyTop >= hot_activity && bodyTop < recommend){
                    $(".floor div").css({
                        "background":"",
                        "color":"#cccccc"
                    });
                    $(".two").css({
                        "background":"#ff666b",
                        "color":"white"
                    });
                }else if(bodyTop >= recommend){
                    $(".floor div").css({
                        "background":"",
                        "color":"#cccccc"
                    });
                    $(".three").css({
                        "background":"#ff666b",
                        "color":"white"
                    });
                }else if(bodyTop>footer_search || bodyTop<buystop){
                    _floor.fadeOut(200);
                }
            },
            loadCarousel: function(){
                // console.log(1);
                $(".banner").carousel({
                    imgs: [
                        // {href:"#",src:"/img/indexImgs/1d4bf4ece8c26.jpg"},
                        {href:"#",src:"/img/indexImgs/2e5899c989167.jpg"},
                        {href:"#",src:"/img/indexImgs/11ed92dcb1563.jpg"},
                        {href:"#",src:"/img/indexImgs/28aa56fcdc5c8.jpg"},
                        {href:"#",src:"/img/indexImgs/dfffc83d97b.jpg"},
                        {href:"#",src:"/img/indexImgs/df3e96598b3.jpg"},
                    ],
                    // width: 855,
					height: 400,
					duration: 3000
                });

                this.hover();
            },

            hover: function(){
                $(".banner").hover(function(){
                    $(".prev,.next").fadeIn(100);
                },function(){
                    $(".prev,.next").fadeOut(100);
                });
            },

            buysLoad: function(){
                $.ajax("http://rap2api.taobao.org/app/mock/86521/60buys")
                .done(function(data){
                    var data = {list : data.res_body.list};

                    var html = template("buys-template",data);
                    $(".buys60").html(html);
                });
            },

            timer: function(){
                var _this = this;
                var top = -324,
                    stop = -180,
                    sflag = 5,
                    flag = 9;
                setInterval(function(){
                    flag--;
                    top = top + 36;
                    var stringtop = top + "px";
                    $(".ge").animate({
                        backgroundPositionY: stringtop
                    },400);

                    if(flag < 0){
                        $(".ge").animate({
                            backgroundPositionY: "-324px"
                        },50);
                        flag = 9;
                        top = -324;
                        sflag--;
                        stop = stop + 36;

                        var strtop = stop + "px";
                        $(".shi").animate({
                            backgroundPositionY: strtop 
                        },400);

                        if(sflag < 0){
                            $(".shi").animate({
                                backgroundPositionY: "-180px"
                            },50);
                            sflag = 5;
                            stop = -180;
                            _this.buysLoad();
                        }
                    }
                },1000);
            },


        });

        new Index();
    })
});